package com.ibiaoke.boss.controller;

import com.ibiaoke.boss.entity.SysUser;
import com.ibiaoke.common.base.BaseController;
import com.ibiaoke.common.base.Page;
import com.ibiaoke.common.response.Res;
import com.ibiaoke.goods.entity.Goods;
import com.ibiaoke.goods.entity.GoodsClass;
import com.ibiaoke.goods.service.GoodsClassService;
import com.ibiaoke.goods.service.GoodsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * Created by Administrator on 2017/6/8.
 */
@Controller
public class BossGoodsController extends BaseController{

    private Logger logger = LoggerFactory.getLogger(this.getClass().getName());
    @Autowired
    private GoodsClassService goodsClassService;
    @Autowired
    private GoodsService goodsService;

    /**
     * 获取商品分类
     *
     * @return res
     */
    @ResponseBody
    @RequestMapping("/listGoodsClass")
    public Res getListGoodsClass() {
        List<GoodsClass> goodsClassList = goodsClassService.listGoodsClass();
        return Res.create().addResponse("goodsClassList", goodsClassList);
    }

    /**
     * 商品查询
     *
     * @param goodsId   商品编号
     * @param status    商品上架状态
     * @param goodsName 商品名称
     * @param cateId    商品分类
     * @return
     */
    @ResponseBody
    @RequestMapping("/listGoods")
    public Res listGoods(Integer goodsId, Integer status, String goodsName, Integer cateId,Integer shopId, Page page) {
        Res res = Res.create();
        List<Goods> goodsList = goodsService.listGoods(goodsId, status, goodsName, cateId, shopId,page);
        return Res.create().addResponse("goodsList", goodsList).addResponse("page", page);
    }
    /**
    *添加商品
    */
    @ResponseBody
    @RequestMapping("/saveGoods")

    public Res saveGoods(Goods goods,String shopIds){
        SysUser user = getLoginUser();
        int saveGoods = goodsService.batchSaveGoods(goods, shopIds);
        logger.info("系统用户{}操作了添加商品的操作",user.getUserName());
        return Res.create().addResponse("saveGoods",saveGoods);
    }

    /**
     * 修改商品删除状态
     * @param goodsId
     * @return
     */
    @ResponseBody
    @RequestMapping("/deleteGoodsById")
    public Res deleteGoodsById(Integer goodsId){
        SysUser user = getLoginUser();
        int deleteResult = goodsService.deleteGoodsById(goodsId);
        logger.info("系统用户{}操作了修改商品删除状态的操作",user.getUserName());
        return Res.create().addResponse("deleteResult",deleteResult);
    }

    /**
     * 批量删除
     * @param goodsIds
     * @return
     */
    @ResponseBody
    @RequestMapping("/batchDeleteGoods")
    public Res batchDeleteGoods(String goodsIds){
        SysUser user = getLoginUser();
        goodsService.batchDeleteGoods(goodsIds);
        logger.info("系统用户{}操作了商品批量删除的操作",user.getUserName());
        return Res.create();
    }


    /**
     * 批量修改商品上下架
     * @param goodsIds
     * @param status
     * @return
     */
    @ResponseBody
    @RequestMapping("/batchUpdateGoodsStatus")
    public Res batchUpdateGoodsStatus(String goodsIds,Integer status){
        SysUser user = getLoginUser();
        int count = goodsService.batchUpdateGoodsStatus(goodsIds,status);
        logger.info("系统用户{}操作了批量修改商品上下架的操作",user.getUserName());
        return  Res.create().addResponse("count",count);
    }

    /**
     * 查询商品根据Id
     * @param goodsId
     * @param shopId
     * @return
     */
    @ResponseBody
    @RequestMapping("/getGoodsByGoodsId")
    public Res getGoodsByGoodsId(Integer goodsId, Integer shopId){
        Goods goods = goodsService.getGoodsByGoodsId(goodsId, shopId);
        return Res.create().addResponse("goods",goods);
    }

    /**
     * 根据商品Id修改商品
     * @param goods
     * @return
     */
    @ResponseBody
    @RequestMapping("/updateGoodsById")
    public Res updateGoodsById(Goods goods){
        SysUser user = getLoginUser();
        goodsService.updateGoodsById(goods);
        logger.info("系统用户{}操作了根据商品Id修改商品的操作",user.getUserName());
        return Res.create();
    }
}
